<?php

/**
 * This is the model class for table "menu".
 *
 * The followings are the available columns in table 'menu':
 * @property string $id
 * @property string $url
 * @property string $name_id
 * @property string $parent_id
 * @property string $position
 *
 * The followings are the available model relations:
 * @property Text $name
 * @property Menu $parent
 * @property Menu[] $menus
 * @property MenuCategoryConnect[] $menuCategoryConnects
 */
class Menu extends AActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Menu the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'menu';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('url, name_id, position', 'required'),
			array('url', 'length', 'max'=>250),
			array('name_id, parent_id, position', 'length', 'max'=>10),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, url, name_id, parent_id, position', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'name' => array(self::BELONGS_TO, 'Text', 'name_id'),
			'parent' => array(self::BELONGS_TO, 'Menu', 'parent_id'),
			'menuCategoryConnects' => array(self::HAS_MANY, 'MenuCategoryConnect', 'menu_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'url' => 'ЧПУ ссылка',
			'name_id' => 'Название',
			'name.string' => 'Название',
			'parent_id' => 'Родитель',
			'parent.name.string'=>'Родитель',
			'position' => 'Position',
			'category'=>'Категория',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		$criteria = $this->getGridCriteria();

		$criteria->compare('id',$this->id,true);
		$criteria->compare('url',$this->url,true);
		$criteria->compare('name_id',$this->name_id,true);
		$criteria->compare('parent_id',$this->parent_id,true);
		$criteria->compare('position',$this->position,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'pagination'=>array(
				'pageSize'=>10,
			)
		));
	}

	protected function getDeleteConfig(){
		return array(
			'name',
			'menuCategoryConnects',
		);
	}
}